home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 10 - 1994 / 10.01 Jan 94 / Scripting Additions / SAPlayMovie.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-29  |  10.8 KB  |  339 lines  |  [TEXT/MPS ]

  1. ////////////////////////////////////////////////////////////////////
  2. //
  3. //      SAPlayMovie.c written by Donald O. Olson
  4. //      A simple QuickTime Scripting Addition written
  5. //        to illustrate writing Scripting Additions.
  6. //
  7. //         Copyright ®1993 Donald O. Olson
  8. //        All rights reserved.
  9. //
  10. //////////////////////////////////////////////////////////////////
  11.  
  12. // Our includes
  13. #include <Movies.h>
  14. #include <Memory.h>
  15. #include <Fonts.h>
  16. #include <OSEvents.h>
  17. #include <limits.h> 
  18. #include <Menus.h>
  19. #include <Processes.h>
  20. #include <String.h>
  21. #include <Resources.h>
  22. #include <Packages.h>
  23. #include <AppleEvents.h>
  24. #include <Errors.h>
  25. #include <GestaltEqu.h>
  26. #include <Files.h>   
  27.  
  28. // Our optional parameters keyword
  29. #define    keyLocation    'LOCA'
  30.  
  31. // Our rectangle records keywords
  32. #define    keyRight    'RGHT'    
  33. #define    keyLeft        'LEFT'    
  34. #define    keyTop        'TOP '    
  35. #define    keyBottom    'BOTM'    
  36.  
  37. #define kLeft            1    // Our list of points are in the
  38. #define kTop            2    // order shown
  39. #define kRight            3
  40. #define kBottom            4
  41. #define kDefaultOffset    100    // Used to position movie on screen
  42. #define kBogusNumber    UINT_MAX
  43.  
  44. /*
  45.      Our prototypes, could be in a .h file but are included here 
  46.      for ease of use.
  47. */
  48.  
  49. OSErr PlayTheMovie(    FSSpec myFSSpec, AEDesc theLocationDesc);
  50. OSErr SetMovieRect(    AEDesc theLocationDesc, Movie theMovie,
  51.                     Rect *ourRect);
  52.  
  53. ////////////////////////////////////////////////////////////////////
  54. //
  55. //     main()
  56. //    The entry to our Scripting Addition.
  57. //    Remember to declare it pascal!!
  58. //
  59. //////////////////////////////////////////////////////////////////
  60.  
  61. pascal OSErr main(    AppleEvent *theEvent, 
  62.                     AppleEvent *theReply, 
  63.                     long theRefCon)
  64. {    
  65.     OSErr        theErr = noErr;
  66.     FSSpec        theFSSpec;
  67.     DescType     typeCode;
  68.     long        theGestaltReturn = 0;
  69.     Size         actualSize;
  70.     AEDesc         theLocationDesc;
  71.     
  72.     /* Is QuickTime present? */
  73.     theErr = Gestalt(gestaltQuickTime, &theGestaltReturn);
  74.     if(theErr) return theErr; // If not, bail.
  75.     
  76.       /*
  77.           Grab the movie file's path from the direct parameter.
  78.           We declared the direct parameter to be an alias in our
  79.           aete. Since the AEM will coerce an alias to a FSSpec for
  80.           us, and that's what the OpenMovieFile call wants, we'll
  81.           ask for it as an FSSpec.
  82.       */
  83.                                   
  84.     theErr = AEGetParamPtr(    theEvent, keyDirectObject, 
  85.                             typeFSS, &typeCode, (Ptr)&theFSSpec, 
  86.                             sizeof(FSSpec), &actualSize);
  87.     if(theErr) return theErr;
  88.  
  89.     /*
  90.         Now get the location parameter, if it's present.
  91.         We don't check errors for this call since the AEM
  92.         will return the descriptor with the descriptorType
  93.         field set to typeNull if there is an error. We check 
  94.         for NULL in the PlayTheMovie function.
  95.     */    
  96.     
  97.     theErr = AEGetParamDesc(    theEvent, keyLocation,
  98.                                 typeWildCard, &theLocationDesc);                           
  99.     
  100.     /* Start up the movie tools */
  101.     if(EnterMovies()) return theErr;    // Bail on error.
  102.     
  103.     theErr = PlayTheMovie(theFSSpec, theLocationDesc);        
  104.     
  105.     ExitMovies();    // Close our connection to the movie tools
  106.     
  107.     return theErr;    // And return our error.
  108. }
  109.  
  110. ////////////////////////////////////////////////////////////////////
  111. //
  112. //     PlayTheMovie() Opens and plays Movie File
  113. //    This code is based on the SimplePlayer sample
  114. //    that comes with the QuickTime Developers Disk.
  115. //
  116. //////////////////////////////////////////////////////////////////
  117.  
  118. OSErr  PlayTheMovie(FSSpec myFSSpec, AEDesc theLocationDesc)
  119. {
  120.     Movie        theMovie;
  121.     Rect        dispBounds;
  122.     WindowPtr    movieWindow = NULL;
  123.     OSErr        theErr = noErr;
  124.     short        resRefNum;        
  125.     long        duration = 60, finalTick;
  126.     
  127.     /* Open the movie file */
  128.     if(OpenMovieFile(&(myFSSpec), &resRefNum, 0)) {
  129.         SysBeep(1);    // Signal our error
  130.         return theErr;    // And bail 
  131.     }
  132.     
  133.     if(NewMovieFromFile( &theMovie, resRefNum, 
  134.                     NULL, NULL,0, NULL )) {
  135.         SysBeep(1);    // Signal our error
  136.         return theErr;        // And bail
  137.     }
  138.         
  139.     /* Get the bounds for the movie. */
  140.     
  141.     GetMovieBox( theMovie, &dispBounds);
  142.     
  143.     /* 
  144.         If the user passed in a location or size for the window,
  145.         let's grab it now.
  146.     */
  147.     
  148.     if(theLocationDesc.dataHandle != NULL) {
  149.         // Use the values sent to us by the user.
  150.         theErr = SetMovieRect(    theLocationDesc, theMovie,                             
  151.                                 &dispBounds);
  152.         if(theErr) return theErr;
  153.     } else {    
  154.         OffsetRect(    &dispBounds,-dispBounds.left,-                        
  155.                     dispBounds.top);
  156.         SetMovieBox(theMovie, &dispBounds);    
  157.         // Make sure window not under menu bar
  158.         OffsetRect(&dispBounds,kDefaultOffset,kDefaultOffset);    
  159.     }
  160.     
  161.     /* 
  162.         Any time you are going to put up a dialog be sure to         
  163.         call AEInteractWithUser. The AppleEvent Manager will         
  164.         take care of posting notification and/or layer switching         
  165.         as needed.
  166.     */
  167.     
  168.     theErr = AEInteractWithUser(kAEDefaultTimeout, NULL, NULL);
  169.     if(theErr) return theErr;
  170.     
  171.     /* Set up our window */
  172.     movieWindow = NewCWindow(    0L, &dispBounds,
  173.                         (StringPtr)myFSSpec.name,
  174.                         true,0,(WindowPtr)-1L,false,0L);
  175.     if(movieWindow == NULL) {
  176.         // Whoops, no window so BAIL;
  177.         SysBeep(1);
  178.         return memFullErr;
  179.     }    
  180.     
  181.     ShowWindow(movieWindow);        // Make the window visible
  182.     SetPort(movieWindow);        // Set the part
  183.     SetMovieGWorld(theMovie,NULL,NULL);    // Set up the movie world
  184.     
  185.     /* Now we're ready to play the movie */
  186.     GoToBeginningOfMovie(theMovie);// Rewind movie
  187.     PrerollMovie(theMovie,0,0);    // Get the movie ready to play 
  188.     SetMovieActive(theMovie,true);    // Activate movie 
  189.     StartMovie(theMovie);        // Start playing
  190.     
  191.     /* 
  192.         Play the movie to the end unless the mouse button has         
  193.         been pressed.
  194.     */
  195.     while ( !IsMovieDone(theMovie) && !Button())
  196.         MoviesTask(theMovie,0);
  197.         
  198.     FlushEvents(everyEvent, 0);    // Clean up spurious events
  199.     Delay(duration, &finalTick);    // Hold on the final fram
  200.     /* Clean up and go home */
  201.     DisposeMovie(theMovie);        // Get rid of the movie 
  202.     CloseMovieFile(resRefNum);    // Close movie file
  203.     DisposeWindow(movieWindow);    // And dispose our window
  204.     return theErr;
  205. }
  206.  
  207. ////////////////////////////////////////////////////////////////////
  208. //    SetMovieRect
  209. //     Set the position and bounding rectangle of our movie window.
  210. //
  211. //////////////////////////////////////////////////////////////////
  212.  
  213. OSErr SetMovieRect(    AEDesc theLocationDesc, Movie theMovie, 
  214.                     Rect *ourRect) {
  215.     long         numberOfListItems = 0;
  216.     OSErr         theErr = noErr;
  217.     AEKeyword     theAEKeyword;
  218.     DescType     typeCode;
  219.     Size         actualSize;
  220.     long        pointLeft, pointTop,
  221.                 pointRight = kBogusNumber,     // We use pointRight to see if
  222.                 pointBottom;                // if we've gotten a point
  223.                                             // or a rectangle
  224.  
  225.     /* Did we get passed a list? */
  226.     if(theLocationDesc.descriptorType == typeAEList) {
  227.         
  228.         /* Get the data handle size to determine if point or rect */
  229.         theErr = AECountItems(    &theLocationDesc,
  230.                               &numberOfListItems);
  231.          if(theErr) return theErr; // Bail on error!        
  232.         /* Must be two or four items in list */
  233.           if(numberOfListItems != 2 && numberOfListItems != 4) 
  234.             return paramErr;
  235.         
  236.         /* If it's a point, just move the window. */
  237.         if(numberOfListItems == 2) {
  238.             theErr = AEGetNthPtr(    &theLocationDesc, kLeft,                             
  239.                                 typeLongInteger,
  240.                                      &theAEKeyword, &typeCode,
  241.                                      (Ptr)&pointLeft,                             
  242.                                      sizeof(pointLeft),                             
  243.                                      &actualSize);
  244.             if(theErr) return theErr; // Just in case…               
  245.                         
  246.             theErr = AEGetNthPtr(    &theLocationDesc, kTop,                             
  247.                                     typeLongInteger,
  248.                                      &theAEKeyword, &typeCode,
  249.                                      (Ptr)&pointTop,                         
  250.                                      sizeof(pointTop),
  251.                                      &actualSize);
  252.             if(theErr) return theErr; // Just in case…    
  253.             
  254.         } else if(numberOfListItems == 4) { // It's a rectangle
  255.                 theErr = AEGetNthPtr(    &theLocationDesc, kLeft,                             
  256.                                         typeLongInteger,
  257.                                          &theAEKeyword, &typeCode,
  258.                                          (Ptr)&pointLeft,                             
  259.                                          sizeof(pointLeft),                             
  260.                                          &actualSize);
  261.                 if(theErr) return theErr; // Just in case…     
  262.                                         
  263.                 theErr = AEGetNthPtr(    &theLocationDesc, kTop,                             
  264.                                         typeLongInteger,
  265.                                          &theAEKeyword, &typeCode,
  266.                                          (Ptr)&pointTop,                             
  267.                                          sizeof(pointTop),                             
  268.                                          &actualSize);
  269.                 if(theErr) return theErr; // Just in case…    
  270.                                              
  271.                 theErr = AEGetNthPtr(    &theLocationDesc, kRight,                             
  272.                                         typeLongInteger,
  273.                                          &theAEKeyword, &typeCode,
  274.                                          (Ptr)&pointRight,                             
  275.                                          sizeof(pointRight),                             
  276.                                          &actualSize);
  277.                 if(theErr) return theErr; // Just in case… 
  278.                                             
  279.                 theErr = AEGetNthPtr(    &theLocationDesc, kBottom,                             
  280.                                         typeLongInteger,
  281.                                          &theAEKeyword, &typeCode,
  282.                                          (Ptr)&pointBottom,                             
  283.                                          sizeof(pointBottom),                             
  284.                                          &actualSize);
  285.                 if(theErr) return theErr; // Just in case…        
  286.                    
  287.             } 
  288.         /* Is it a record? */
  289.         } else if(theLocationDesc.descriptorType == typeAERecord) { 
  290.             /* Get the points out by key names */
  291.             
  292.             theErr = AEGetKeyPtr(    &theLocationDesc, keyLeft,                             
  293.                                     typeLongInteger,
  294.                                      &typeCode, (Ptr)&pointLeft,                             
  295.                                      sizeof(pointLeft),                             
  296.                                      &actualSize);
  297.               if(theErr) return theErr; // Must have these two
  298.     
  299.               theErr = AEGetKeyPtr(    &theLocationDesc, keyTop,                             
  300.                                       typeLongInteger,
  301.                                       &typeCode, (Ptr)&pointTop,                             
  302.                                       sizeof(pointTop),                             
  303.                                       &actualSize);
  304.             if(theErr) return theErr; // Must have these two
  305.             
  306.             theErr = AEGetKeyPtr(    &theLocationDesc, keyRight,                             
  307.                                     typeLongInteger,
  308.                                      &typeCode, (Ptr)&pointRight,                             
  309.                                      sizeof(pointRight),                             
  310.                                      &actualSize);
  311.             // Ignore this error
  312.  
  313.               theErr = AEGetKeyPtr(    &theLocationDesc, keyBottom,                         
  314.                                       typeLongInteger,
  315.                                      &typeCode, (Ptr)&pointBottom,
  316.                                      sizeof(pointBottom),
  317.                                      &actualSize);
  318.             // Ignore this error too, but clear our variable
  319.             theErr = noErr;
  320.         }
  321.         
  322.     if(pointRight == kBogusNumber)    // We got a new origin...
  323.         SetRect(    ourRect, pointLeft, pointTop,                     
  324.                     (ourRect->right - ourRect->left) + pointLeft,                 
  325.                     (ourRect->bottom - ourRect->top) + pointTop);
  326.     else                            // We got a new rectangle...
  327.         SetRect(    ourRect, pointLeft, pointTop,                     
  328.                     pointRight, pointBottom);
  329.  
  330.     /* Set topleft to 0,0 */
  331.     OffsetRect(ourRect,-ourRect->left,-ourRect->top); 
  332.     
  333.     /* Set the movie box to the new rect. */
  334.     SetMovieBox(theMovie, ourRect); 
  335.  
  336.     OffsetRect(ourRect,pointLeft, pointTop);
  337.     
  338.     return theErr;
  339. }